<template>
	<view>
		<view class="head">
			<block v-if="userInfo.id">
				<view class="userinfo" @click="toUcenter">
					<u-avatar :customStyle="{marginRight:'20rpx'}" :src="$utils.imgPath(userInfo.avatar)"></u-avatar>
					<view class="username">
						<text>{{ userInfo.nickname }}</text>
					</view>
					<view class="arrow-right">
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
			</block>
			<block v-else>
				<view class="btn-login">
					<q-button @click="$store.dispatch('mnpLogin');" plain>登录</q-button>
				</view>
			</block>
			<u-grid v-if="userInfo" :col="3" :border="false" style="margin: 20rpx 0;" @click="toNav">
				<u-grid-item name="/pages/my/fans">
					<text>{{ userInfo.fans_num || 0 }}</text>
					<view class="grid-text">粉丝</view>
				</u-grid-item>
				<u-grid-item name="/pages/my/follow">
					<text>{{ userInfo.follow_num || 0 }}</text>
					<view class="grid-text">关注</view>
				</u-grid-item>
				<u-grid-item name="/pages/my/post">
					<text>{{ userInfo.post_num || 0 }}</text>
					<view class="grid-text">帖子</view>
				</u-grid-item>
			</u-grid>
		</view>
		<!-- 社区服务 -->
		<view class="block-wrap">
			<view class="block-title">社区服务</view>
			<u-grid :col="4" :border="false" style="margin: 20rpx 0;" @click="toNav">
				<u-grid-item name="/pages/my/post">
					<image class="gn-icon" src="/static/m_3.png"></image>
					<view class="grid-text">我的帖子</view>
				</u-grid-item>

				<u-grid-item name="/pages/my/thumb">
					<image class="gn-icon" src="/static/m_4.png"></image>
					<view class="grid-text">点赞的帖子</view>
				</u-grid-item>

				<u-grid-item name="/pages/topic/add">
					<image class="gn-icon" src="/static/m_6.png"></image>
					<view class="grid-text">创建圈子</view>
				</u-grid-item>
			</u-grid>
		</view>
		<!-- 帮助 -->
		<view class="block-wrap">
			<view class="block-title">帮助</view>
			<view class="grid-wrap">
				<view class="grid-item">
					<button open-type="share" class="u-reset-button">
						<image class="gn-icon" style="margin-bottom: unset;" src="/static/m_1.png"></image>
						<view class="grid-text">发给好友</view>
					</button>
				</view>
				<view class="grid-item">
					<button open-type="contact" class="u-reset-button">
						<image class="gn-icon" style="margin-bottom: unset;" src="/static/m_8.png"></image>
						<view class="grid-text">帮助与反馈</view>
					</button>
				</view>
				<view class="grid-item">
					<button @click="onClear" class="u-reset-button">
						<image class="gn-icon" style="margin-bottom: unset;" src="/static/m_9.png"></image>
						<view class="grid-text">清除缓存</view>
					</button>
				</view>
			</view>
		</view>
		<q-tabbar :selected="4"></q-tabbar>
	</view>
</template>

<script>
	export default {
		data() {
			return {

			};
		},
		computed: {
			userInfo() {
				return this.$store.state.user.userInfo;
			}
		},
		onShow() {
			if (this.userInfo && this.userInfo.id) {
				this.getUserInfo();
				this.$store.dispatch('getMessage');
			} else {
				this.$store.commit("logout")
			}
			// 更新自定义tabbar选中状态
			if (typeof this.getTabBar === 'function' && this.getTabBar()) {
				this.getTabBar().setData({
					selected: 3
				})
			}
		},
		onLoad() {},
		methods: {
			onClear() {
				let that = this;
				uni.showModal({
					title: '提示',
					content: '是否清除登录信息以及数据缓存?',
					success: function(res) {
						if (res.confirm) {
							uni.clearStorageSync();
							that.$store.commit("logout");
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			getUserInfo() {
				this.$H.post('user/getInfo').then(res => {
					let userInfo = res.result;
					this.$store.commit("set_info", userInfo);
				});
			},
			toUcenter() {
				uni.navigateTo({
					url: '/pages/user/edit-info/edit'
				});
			},
			toNav(url) {
				uni.navigateTo({
					url: url
				});
			}
		}
	};
</script>

<style>
	page {
		background-color: #f5f5f5;
	}
</style>
<style lang="scss" scoped>
	.head {
		padding: 20rpx;
		background-color: #fff;
		margin-bottom: 20rpx;
	}

	.userinfo {
		display: flex;
		align-items: center;
		padding: 20rpx;

		.arrow-right {
			margin-left: auto;
		}
	}

	.userinfo .username {
		display: flex;
		flex-direction: column;
	}

	.grid-text {
		color: #999;
		font-size: 12px;
		margin-bottom: 20rpx;
	}


	.btn-login {
		margin: 40rpx 0;
	}

	.gn-icon {
		width: 60rpx;
		height: 60rpx;
		margin-bottom: 20rpx;
	}

	/*服务按钮*/
	.btn-wrap {
		display: flex;
		margin-top: 30rpx;
	}

	.btn-wrap .btn-contact {
		background-color: #fff;
		margin-left: 15rpx;
		margin-right: 15rpx;
		padding: 20rpx;
		line-height: unset;
		font-size: 12px;
		color: #999;
	}

	.btn-wrap .btn-contact:active {
		background-color: #f5f5f5;
	}

	.btn-wrap .btn-contact .txt {
		margin-top: 20rpx;
	}

	.btn-wrap .btn-contact::after {
		border: unset;
		position: unset;
	}

	.icon-size {
		font-size: 50rpx;
	}

	.block-wrap {
		background-color: #fff;
		border-radius: 20rpx;
		margin: 20rpx;
		overflow: hidden;

		.block-title {
			background-color: #fff;
			padding: 20rpx;
		}
	}

	.grid-wrap {
		display: grid;
		grid-template-columns: repeat(4, 1fr);

		.grid-item {
			text-align: center;
			position: relative;

			.grid-text {
				color: #999;
				font-size: 12px;
				margin-bottom: 20rpx;
			}

			.message-num {
				position: absolute;
				right: 20rpx;
				top: 0;
				padding: 5rpx;
				width: 35rpx;
				height: 35rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 50%;
				background-color: red;
				color: #fff;
				font-size: 24rpx;
			}
		}
	}
</style>